﻿using System;
using System.Drawing;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
using CoreLib;
using CoreLib.Plugins;
using CoreLib.Components;
using Primitives3D;
namespace Gizmo
{
   public class Transitions:DrawablePlugin
    {
       enum TransitionModeEnum
       {
           Move,
           Scale,
           Rotate,
       }
       // store a wireframe rasterize state
     
        TransitionModeEnum TransitionMode;
 
        Box cube;
  
        Vector2 MousePositionPressed=Vector2.Zero;
        Vector2 MousePositionMove=Vector2.Zero;
        Game game;
        public Transitions(Game gameparameter)
            : base(gameparameter)
       {
           game = gameparameter;
      
           RotationMatrix = Matrix.Identity;
          PositionMatrix = Matrix.Identity;

          PluginName = "Transition";
          PluginDescription = "Moving, scaling and rotation of 3D objects.";
          
          PluginCommands.Add(new PluginCommands((Bitmap)Image.FromFile(System.Environment.CurrentDirectory + "/plugins/Movebutton.png"),"Move"));
          PluginCommands.Add(new PluginCommands((Bitmap)Image.FromFile(System.Environment.CurrentDirectory + "/plugins/Scalebutton.png"), "Scale"));
          PluginCommands.Add(new PluginCommands((Bitmap)Image.FromFile(System.Environment.CurrentDirectory + "/plugins/Rotatebutton.png"), "Rotate"));

    
          ScaleMatrix = Matrix.CreateScale(1, 1, 1);
          PositionMatrix = Matrix.CreateTranslation(0, 0, 0);
          Enabled = true;
        }
        public override void Draw(GameTime gameTime)
        {
    
            World = PositionMatrix*ScaleMatrix;
            if(cube!=null)
            //cube.Draw(World, Engine.CurrentCamera.View, CCameraComponent.Projection, new Microsoft.Xna.Framework.Color(255, 255, 255));
           
            base.Draw(gameTime);
     
        }
        protected override void LoadContent()
        {
     
            base.LoadContent();
        }
        public override void Update(GameTime gameTime)
        {
            Vector3 TransitionVector=Vector3.Zero;
            if (CoreLib.Mouse.LeftButton == Microsoft.Xna.Framework.Input.ButtonState.Pressed && Globals.SelectedObject!=null)
            {
             
                float r = 0;
                foreach (ModelMesh mesh in Globals.SelectedObject.model.Meshes)
                {
                  if(mesh.BoundingSphere.Radius>=r)
                  r=  mesh.BoundingSphere.Radius;
                }
                cube = new Box(game);
                cube.Size = r;
                cube.CreateVertices();
                if(MousePositionPressed==Vector2.Zero)
                {
                    MousePositionPressed.X = CoreLib.Mouse.X;
                    MousePositionPressed.Y = CoreLib.Mouse.Y;
                }
                try
                {
                    MousePositionMove.X = CoreLib.Mouse.X;
                    MousePositionMove.Y = CoreLib.Mouse.Y;
                    DrawablePlugin plugin = (DrawablePlugin)Globals.SelectedObject;
                    switch (TransitionMode)
                    {
                        case TransitionModeEnum.Move:

                                        Ray cursorRay = Engine.Cursor.CalculateCursorRay( CCameraComponent.Projection, Engine.CurrentCamera.View);
                    TransitionVector = plugin.PositionVector3;
           
                    TransitionVector.X = TransitionVector.X - (MousePositionPressed.X-MousePositionMove.X)/100;
                    TransitionVector.Y = TransitionVector.Y - (MousePositionPressed.Y - MousePositionMove.Y) / 100;
                    plugin.PositionVector3 = cursorRay.Position;
                            break;
                        case TransitionModeEnum.Scale:
                            break;
                        case TransitionModeEnum.Rotate:
                             TransitionVector = plugin.RotationVector3;
           
                    TransitionVector.X = TransitionVector.X - (MousePositionPressed.X-MousePositionMove.X)/100;
                    TransitionVector.Y = TransitionVector.Y - (MousePositionPressed.Y - MousePositionMove.Y) / 100;
                    plugin.RotationVector3 = TransitionVector;
                            break;
                        default:
                            break;
                    }

                    if (CoreLib.Mouse.LeftButton == Microsoft.Xna.Framework.Input.ButtonState.Released)
                    {
                        MousePositionPressed = Vector2.Zero;
                        MousePositionMove = Vector2.Zero;
                    }
                    Vector3 vec = Globals.Project(Globals.SelectedObject.PositionVector3, Engine.CurrentCamera.World);

                    ScaleMatrix =Globals.SelectedObject.ScaleMatrix;
                    PositionMatrix = Globals.SelectedObject.PositionMatrix;
        
                }
                catch (System.InvalidCastException)
                {
                }
                catch (System.Exception)
                {

                    throw;
                }
            }

            base.Update(gameTime);
        }
    }
}
